package archivoConfiguracionXML.vistas;

import archivoConfiguracionXML.*;
import archivoConfiguracionXML.logica.CreaXML;
import archivoConfiguracionXML.logica.Des;
import archivoConfiguracionXML.logica.EncriptaMD5;
import archivoConfiguracionXML.logica.LeerXml;

/*
 * Marco Guida:
 * Si hay tiempo controlar la entrada que acepte solo letras mayusculas y minusculas
 * y numeros, se crea un array de caracteres permitidos, luego hay una funcion que
 * devuelve true si hay este caracter en el array.
 * */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Dimension;
import javax.swing.SwingConstants;
import java.awt.ComponentOrientation;
import javax.swing.JFormattedTextField;

import sun.org.mozilla.javascript.internal.Node;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Iterator;

public class VistaCodificadora extends JFrame {

	private JPanel contentPane;
	private JTextField etxtCodigo;
	private final int NUM_CARACTERES_CIF_Y_NIE = 9;// En realidad son 9 pero de
													// 0 a 8
													// van 9 caracteres
	private final int NUM_CARACTERES_NIF = 8;
	private archivoConfiguracionXML.logica.Logica logicaCodificador = new archivoConfiguracionXML.logica.Logica();
	private boolean esCIFValido = false;
	private JLabel lblIntroducir;
	private JLabel lblRazonSocial;

	private JLabel lblDireccion;
	private JLabel lblTel1;
	private JLabel lblTel2;
	private JLabel lblFax;
	private JLabel lblEmail1;
	private JLabel lblEmail2;
	private JLabel lblWeb;
	private JLabel lblNumFactInicial;
	private JLabel lblCampo;
	private JFormattedTextField etxtRazonSocial;
	private JFormattedTextField etxtDireccion;
	private JFormattedTextField etxtTelefono1;
	private JFormattedTextField etxtTelefono2;
	private JFormattedTextField etxtFax;
	private JFormattedTextField etxtEmail1;
	private JFormattedTextField etxtEmail2;
	private JFormattedTextField etxtWeb;
	private JFormattedTextField etxtNumFacturaInicial;
	private JButton btnLeerXML;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VistaCodificadora frame = new VistaCodificadora();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public VistaCodificadora() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 348);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{70, 124, 78, 89, 0};
		gbl_contentPane.rowHeights = new int[]{23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
				
						lblIntroducir = new JLabel("Introducir CIF, NIF o NIE:");
						lblIntroducir.setHorizontalTextPosition(SwingConstants.RIGHT);
						lblIntroducir.setHorizontalAlignment(SwingConstants.RIGHT);
						GridBagConstraints gbc_lblIntroducir = new GridBagConstraints();
						gbc_lblIntroducir.anchor = GridBagConstraints.EAST;
						gbc_lblIntroducir.insets = new Insets(0, 0, 5, 5);
						gbc_lblIntroducir.gridx = 1;
						gbc_lblIntroducir.gridy = 0;
						contentPane.add(lblIntroducir, gbc_lblIntroducir);
				
						etxtCodigo = new JTextField();
						etxtCodigo.setMinimumSize(new Dimension(100, 20));
						etxtCodigo.setColumns(9);
						
								etxtCodigo.addKeyListener(new KeyAdapter() {
									@Override
									public void keyReleased(KeyEvent evento) {
										// System.out.println(etxtCIF.getText().length());
										// // if (etxtCIF.getText().length() == NUM_CARACTERES_NIF) {
										// Con 8 caracteres ya puede ser un NIF
										// // }
						
										// System.out.println("Es NIE: "+logicaCodificador.validarNIE(etxtCIF.getText()));
										// System.out.println("Es NIF: "+logicaCodificador.validarNIF(etxtCIF.getText()));
										// System.out.println("Es CIF: "+logicaCodificador.validateCif(etxtCIF.getText()));
						
										//prueba NIF 8 caracteres: 9123456T
										//prueba NIF 9 caracteres: 45073772M
										//prueba CIF Telepizza A78849676
										
										
										if (etxtCodigo.getText().length()==NUM_CARACTERES_CIF_Y_NIE&&logicaCodificador.validarNIE(etxtCodigo.getText()))
										{
											//System.out.println("Es un NIE");	
											lblIntroducir.setText("NIE: ");
											etxtCodigo.setBackground(Color.GREEN);
											habilitaCampos(true);
										} else if (logicaCodificador.validarNIF(etxtCodigo.getText())&&(etxtCodigo.getText().length()==NUM_CARACTERES_NIF||etxtCodigo.getText().length()==NUM_CARACTERES_CIF_Y_NIE))
										{
											//System.out.println("Es un NIF");
											lblIntroducir.setText("NIF: ");
											etxtCodigo.setBackground(Color.GREEN);
											habilitaCampos(true);
										}
										else if(logicaCodificador.validateCif(etxtCodigo.getText())&&etxtCodigo.getText().length()==NUM_CARACTERES_CIF_Y_NIE)
										{
											//System.out.println("Es un CIF");	
											lblIntroducir.setText("CIF: ");
											etxtCodigo.setBackground(Color.GREEN);
											habilitaCampos(true);
										}
										else if(etxtCodigo.getText().length()>NUM_CARACTERES_NIF)
										{
											lblIntroducir.setText("Introducir CIF, NIF o NIE:");
											etxtCodigo.setText(etxtCodigo.getText().substring(0,
													NUM_CARACTERES_CIF_Y_NIE));
											codigoNoValido(evento);
										}
										else {
											lblIntroducir.setText("Introducir CIF, NIF o NIE:");
											//menos de 8 caracteres	
											etxtCodigo.setBackground(Color.WHITE);
											habilitaCampos(false);
										}
						
										
									}
										
							
						
									private void habilitaCampos(boolean isCamposActivos) {
									
										etxtRazonSocial.setText("");
										etxtDireccion.setText("");
										etxtTelefono1.setText("");
										etxtTelefono2.setText("");
										etxtFax.setText("");
										etxtEmail1.setText("");
										etxtEmail2.setText("");
										etxtWeb.setText("");
										etxtNumFacturaInicial.setText("");
										
										etxtRazonSocial.setEditable(isCamposActivos);
										etxtDireccion.setEditable(isCamposActivos);
										etxtTelefono1.setEditable(isCamposActivos);
										etxtTelefono2.setEditable(isCamposActivos);
										etxtFax.setEditable(isCamposActivos);
										etxtEmail1.setEditable(isCamposActivos);
										etxtEmail2.setEditable(isCamposActivos);
										etxtWeb.setEditable(isCamposActivos);
										etxtNumFacturaInicial.setEditable(isCamposActivos);
										
									}



									private void codigoNoValido(KeyEvent evento) {
										etxtCodigo.setBackground(Color.RED);
										habilitaCampos(false);
										getToolkit().beep();
						
									}
								});
								GridBagConstraints gbc_etxtCodigo = new GridBagConstraints();
								gbc_etxtCodigo.anchor = GridBagConstraints.WEST;
								gbc_etxtCodigo.insets = new Insets(0, 0, 5, 5);
								gbc_etxtCodigo.gridx = 2;
								gbc_etxtCodigo.gridy = 0;
								contentPane.add(etxtCodigo, gbc_etxtCodigo);

		
				JButton btnGeneraXML = new JButton("Genera XML");
				btnGeneraXML.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
						 
						 generaXML();
						
					}

					private void generaXML() {
						Des objetoEncriptador= new Des();
						String todosCamposClaro= concatenaCamposParaXML();
						String checkSumMD5=EncriptaMD5.encriptaEnMD5(todosCamposClaro);
						
						String etxtCodigoDes=objetoEncriptador.encrypt(etxtCodigo.getText());
						String etxtRazonSocialDes=objetoEncriptador.encrypt(etxtRazonSocial.getText());
						String etxtDireccionDes=objetoEncriptador.encrypt(etxtDireccion.getText());
						String etxtTelefono1Des=objetoEncriptador.encrypt(etxtTelefono1.getText());
						String etxtTelefono2Des=objetoEncriptador.encrypt(etxtTelefono2.getText());
						String etxtFaxDes=objetoEncriptador.encrypt(etxtFax.getText());
						String etxtEmail1Des=objetoEncriptador.encrypt(etxtEmail1.getText());
						String etxtEmail2Des=objetoEncriptador.encrypt(etxtEmail2.getText());
						String etxtWebDes=objetoEncriptador.encrypt(etxtWeb.getText());
						String etxtNumFacturaInicialDes=objetoEncriptador.encrypt(etxtNumFacturaInicial.getText());
						
						
					/*	System.out.println("Chorizo:"+todosCamposClaro);
						System.out.println("Checksum:"+checkSumMD5);
						System.out.println("Blowfish:");
						 System.out.println(etxtCodigoBlowfish);
						 System.out.println(etxtRazonSocialBlowfish);
						 System.out.println(etxtDireccionBlowfish); 
						 System.out.println(etxtTelefono1Blowfish);
						 System.out.println(etxtTelefono2Blowfish); 
						 System.out.println(etxtFaxBlowfish);
						 System.out.println(etxtEmail1Blowfish);
						 System.out.println(etxtEmail2Blowfish); 
						 System.out.println(etxtWebBlowfish);
						 System.out.println(etxtNumFacturaInicialBlowfish);*/
						
						
						//Codifico los datos y envio las etiquetas XML al generador XML.
						CreaXML generadorXML= new CreaXML();
						
						generadorXML.agregarCampoValor(lblIntroducir.getText().substring(0, 3), etxtCodigoDes);
						generadorXML.agregarCampoValor("RazonSocial",etxtRazonSocialDes);
						generadorXML.agregarCampoValor("Direccion",etxtDireccionDes);
						generadorXML.agregarCampoValor("Tel1",etxtTelefono1Des);
						generadorXML.agregarCampoValor("Tel2",etxtTelefono2Des);
						generadorXML.agregarCampoValor("Fax",etxtFaxDes);
						generadorXML.agregarCampoValor("Email1",etxtEmail1Des);
						generadorXML.agregarCampoValor("Email2",etxtEmail2Des);
						generadorXML.agregarCampoValor("Web",etxtWebDes);
						generadorXML.agregarCampoValor("NumFactInicial",etxtNumFacturaInicialDes);
						generadorXML.agregarCampoValor("CheckSum",checkSumMD5);

						System.out.println(generadorXML.mostrar());
						generadorXML.generaArchivoXML();
						
					}

					private String concatenaCamposParaXML() {
						return etxtCodigo.getText()+
						etxtRazonSocial.getText()+
						etxtDireccion.getText()+
						etxtTelefono1.getText()+
						etxtTelefono2.getText()+
						etxtFax.getText()+
						etxtEmail1.getText()+
						etxtEmail2.getText()+
						etxtWeb.getText()+
						etxtNumFacturaInicial.getText();
					
					}
				});
				GridBagConstraints gbc_btnGeneraXML = new GridBagConstraints();
				gbc_btnGeneraXML.insets = new Insets(0, 0, 5, 0);
				gbc_btnGeneraXML.anchor = GridBagConstraints.NORTHWEST;
				gbc_btnGeneraXML.gridx = 3;
				gbc_btnGeneraXML.gridy = 0;
				contentPane.add(btnGeneraXML, gbc_btnGeneraXML);
				
				btnLeerXML = new JButton("Prueba Lee XML");
				btnLeerXML.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
					/*Node prueba=(Node) LeerXml.leerXML();
					Iterator it= prueba.iterator();
					while (it.hasNext()) {
						Node nodo = (Node) it.next();
						System.out.println(nodo.getString());
					}*/
					
					}
				});
				GridBagConstraints gbc_btnLeerXML = new GridBagConstraints();
				gbc_btnLeerXML.insets = new Insets(0, 0, 5, 0);
				gbc_btnLeerXML.gridx = 3;
				gbc_btnLeerXML.gridy = 1;
				contentPane.add(btnLeerXML, gbc_btnLeerXML);
				
				lblRazonSocial = new JLabel("Razon Social*");
				GridBagConstraints gbc_lblRazonSocial = new GridBagConstraints();
				gbc_lblRazonSocial.anchor = GridBagConstraints.EAST;
				gbc_lblRazonSocial.insets = new Insets(0, 0, 5, 5);
				gbc_lblRazonSocial.gridx = 1;
				gbc_lblRazonSocial.gridy = 2;
				contentPane.add(lblRazonSocial, gbc_lblRazonSocial);
				
				etxtRazonSocial = new JFormattedTextField();
				
				etxtRazonSocial.setEditable(false);
				GridBagConstraints gbc_etxtRazonSocial = new GridBagConstraints();
				gbc_etxtRazonSocial.insets = new Insets(0, 0, 5, 5);
				gbc_etxtRazonSocial.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtRazonSocial.gridx = 2;
				gbc_etxtRazonSocial.gridy = 2;
				contentPane.add(etxtRazonSocial, gbc_etxtRazonSocial);
				etxtRazonSocial.setColumns(10);
				
				lblDireccion = new JLabel("Direcci\u00F3n");
				GridBagConstraints gbc_lblDireccion = new GridBagConstraints();
				gbc_lblDireccion.anchor = GridBagConstraints.EAST;
				gbc_lblDireccion.insets = new Insets(0, 0, 5, 5);
				gbc_lblDireccion.gridx = 1;
				gbc_lblDireccion.gridy = 3;
				contentPane.add(lblDireccion, gbc_lblDireccion);
				
				etxtDireccion = new JFormattedTextField();
				etxtDireccion.setEditable(false);
				etxtDireccion.setColumns(10);
				GridBagConstraints gbc_etxtDireccion = new GridBagConstraints();
				gbc_etxtDireccion.insets = new Insets(0, 0, 5, 5);
				gbc_etxtDireccion.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtDireccion.gridx = 2;
				gbc_etxtDireccion.gridy = 3;
				contentPane.add(etxtDireccion, gbc_etxtDireccion);
				
				lblTel1 = new JLabel("Telefono 1*");
				GridBagConstraints gbc_lblTel1 = new GridBagConstraints();
				gbc_lblTel1.anchor = GridBagConstraints.EAST;
				gbc_lblTel1.insets = new Insets(0, 0, 5, 5);
				gbc_lblTel1.gridx = 1;
				gbc_lblTel1.gridy = 4;
				contentPane.add(lblTel1, gbc_lblTel1);
				
				etxtTelefono1 = new JFormattedTextField();
				etxtTelefono1.setEditable(false);
				etxtTelefono1.setColumns(10);
				GridBagConstraints gbc_etxtTelefono1 = new GridBagConstraints();
				gbc_etxtTelefono1.insets = new Insets(0, 0, 5, 5);
				gbc_etxtTelefono1.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtTelefono1.gridx = 2;
				gbc_etxtTelefono1.gridy = 4;
				contentPane.add(etxtTelefono1, gbc_etxtTelefono1);
				
				lblTel2 = new JLabel("Telefono 2");
				GridBagConstraints gbc_lblTel2 = new GridBagConstraints();
				gbc_lblTel2.anchor = GridBagConstraints.EAST;
				gbc_lblTel2.insets = new Insets(0, 0, 5, 5);
				gbc_lblTel2.gridx = 1;
				gbc_lblTel2.gridy = 5;
				contentPane.add(lblTel2, gbc_lblTel2);
				
				etxtTelefono2 = new JFormattedTextField();
				etxtTelefono2.setEditable(false);
				etxtTelefono2.setColumns(10);
				GridBagConstraints gbc_etxtTelefono2 = new GridBagConstraints();
				gbc_etxtTelefono2.insets = new Insets(0, 0, 5, 5);
				gbc_etxtTelefono2.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtTelefono2.gridx = 2;
				gbc_etxtTelefono2.gridy = 5;
				contentPane.add(etxtTelefono2, gbc_etxtTelefono2);
				
				lblFax = new JLabel("Fax");
				GridBagConstraints gbc_lblFax = new GridBagConstraints();
				gbc_lblFax.anchor = GridBagConstraints.EAST;
				gbc_lblFax.insets = new Insets(0, 0, 5, 5);
				gbc_lblFax.gridx = 1;
				gbc_lblFax.gridy = 6;
				contentPane.add(lblFax, gbc_lblFax);
				
				etxtFax = new JFormattedTextField();
				etxtFax.setEditable(false);
				etxtFax.setColumns(10);
				GridBagConstraints gbc_etxtFax = new GridBagConstraints();
				gbc_etxtFax.insets = new Insets(0, 0, 5, 5);
				gbc_etxtFax.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtFax.gridx = 2;
				gbc_etxtFax.gridy = 6;
				contentPane.add(etxtFax, gbc_etxtFax);
				
				lblEmail1 = new JLabel("Email 1*");
				GridBagConstraints gbc_lblEmail1 = new GridBagConstraints();
				gbc_lblEmail1.anchor = GridBagConstraints.EAST;
				gbc_lblEmail1.insets = new Insets(0, 0, 5, 5);
				gbc_lblEmail1.gridx = 1;
				gbc_lblEmail1.gridy = 7;
				contentPane.add(lblEmail1, gbc_lblEmail1);
				
				etxtEmail1 = new JFormattedTextField();
				etxtEmail1.setEditable(false);
				etxtEmail1.setColumns(10);
				GridBagConstraints gbc_etxtEmail1 = new GridBagConstraints();
				gbc_etxtEmail1.insets = new Insets(0, 0, 5, 5);
				gbc_etxtEmail1.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtEmail1.gridx = 2;
				gbc_etxtEmail1.gridy = 7;
				contentPane.add(etxtEmail1, gbc_etxtEmail1);
				
				lblEmail2 = new JLabel("Email 2");
				GridBagConstraints gbc_lblEmail2 = new GridBagConstraints();
				gbc_lblEmail2.anchor = GridBagConstraints.EAST;
				gbc_lblEmail2.insets = new Insets(0, 0, 5, 5);
				gbc_lblEmail2.gridx = 1;
				gbc_lblEmail2.gridy = 8;
				contentPane.add(lblEmail2, gbc_lblEmail2);
				
				etxtEmail2 = new JFormattedTextField();
				etxtEmail2.setEditable(false);
				etxtEmail2.setColumns(10);
				GridBagConstraints gbc_etxtEmail2 = new GridBagConstraints();
				gbc_etxtEmail2.insets = new Insets(0, 0, 5, 5);
				gbc_etxtEmail2.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtEmail2.gridx = 2;
				gbc_etxtEmail2.gridy = 8;
				contentPane.add(etxtEmail2, gbc_etxtEmail2);
				
				lblWeb = new JLabel("Web*");
				GridBagConstraints gbc_lblWeb = new GridBagConstraints();
				gbc_lblWeb.anchor = GridBagConstraints.EAST;
				gbc_lblWeb.insets = new Insets(0, 0, 5, 5);
				gbc_lblWeb.gridx = 1;
				gbc_lblWeb.gridy = 9;
				contentPane.add(lblWeb, gbc_lblWeb);
				
				etxtWeb = new JFormattedTextField();
				etxtWeb.setEditable(false);
				etxtWeb.setColumns(10);
				GridBagConstraints gbc_etxtWeb = new GridBagConstraints();
				gbc_etxtWeb.insets = new Insets(0, 0, 5, 5);
				gbc_etxtWeb.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtWeb.gridx = 2;
				gbc_etxtWeb.gridy = 9;
				contentPane.add(etxtWeb, gbc_etxtWeb);
				
				lblNumFactInicial = new JLabel("Numero factura inicial*");
				GridBagConstraints gbc_lblNumFactInicial = new GridBagConstraints();
				gbc_lblNumFactInicial.anchor = GridBagConstraints.EAST;
				gbc_lblNumFactInicial.insets = new Insets(0, 0, 5, 5);
				gbc_lblNumFactInicial.gridx = 1;
				gbc_lblNumFactInicial.gridy = 10;
				contentPane.add(lblNumFactInicial, gbc_lblNumFactInicial);
				
				etxtNumFacturaInicial = new JFormattedTextField();
				etxtNumFacturaInicial.setEditable(false);
				etxtNumFacturaInicial.setColumns(10);
				GridBagConstraints gbc_etxtNumFacturaInicial = new GridBagConstraints();
				gbc_etxtNumFacturaInicial.insets = new Insets(0, 0, 5, 5);
				gbc_etxtNumFacturaInicial.fill = GridBagConstraints.HORIZONTAL;
				gbc_etxtNumFacturaInicial.gridx = 2;
				gbc_etxtNumFacturaInicial.gridy = 10;
				contentPane.add(etxtNumFacturaInicial, gbc_etxtNumFacturaInicial);
				
				lblCampo = new JLabel("* = campo obligatorio");
				GridBagConstraints gbc_lblCampo = new GridBagConstraints();
				gbc_lblCampo.gridx = 3;
				gbc_lblCampo.gridy = 11;
				contentPane.add(lblCampo, gbc_lblCampo);
	}

	public JLabel getLblIntroducir() {
		return lblIntroducir;
	}
}
